package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.UserDto;
import com.zzyl.entity.SysUser;
import com.zzyl.entity.User;
import com.zzyl.entity.UserRole;
import com.zzyl.mapper.UserMapper;
import com.zzyl.mapper.UserRoleMapper;
import com.zzyl.service.SysUserService;
import com.zzyl.utils.UserThreadLocal;
import com.zzyl.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class SysUserServiceImpl implements SysUserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private UserRoleMapper userRoleMapper;

    @Override
    @Transactional
    public Boolean addUser(UserDto userDto) {
        // TODO: 实现添加用户的逻辑
        //将Dto层转为实体类
        User user = BeanUtil.toBean(userDto, User.class);
        user.setPassword(DigestUtils.md5DigestAsHex("888itcast.CN764%...".getBytes()));
        user.setDataState("0");
        //先设置第一张表的数据
        userMapper.insert(user);
        Long userId = user.getId();
        Set<String> roleVoIds = userDto.getRoleVoIds();
        List<UserRole> userRoles = roleVoIds.stream().map(roleVoId -> {
            UserRole userRole = new UserRole();
            userRole.setUserId(userId);
            userRole.setDataState("0");
            userRole.setRoleId(Long.valueOf(roleVoId));
            return userRole;
        }).collect(Collectors.toList());
        userRoleMapper.batchInsert(userRoles);
        return true;

    }

    @Override
    @Transactional
    public void deleteUser(String userIds) {
        // TODO: 实现删除用户的逻辑
        Long userId =Long.valueOf(userIds);
        userMapper.deleteByPrimaryKey(userId);
        userRoleMapper.deleteByPrimaryKey(userId);
    }

    @Override
    @Transactional
    public Boolean updateUser(UserDto UserDto) {
        User User = BeanUtil.toBean(UserDto, User.class);

        userMapper.updateByPrimaryKeySelective(User);
        //获取用户id
        Long userId = User.getId();
       //根据用户id先删除中间表
        userRoleMapper.deleteByPrimaryKey(userId);
        //获取用户角色ids
        Set<String> roleVoIds = UserDto.getRoleVoIds();
        List<UserRole> userRoles = roleVoIds.stream().map(roleVoId -> {
            UserRole userRole = new UserRole();
            userRole.setUserId(userId);
            userRole.setRoleId(Long.valueOf(roleVoId));
            return userRole;
        }).collect(Collectors.toList());
        userRoleMapper.batchInsert(userRoles);
        return true;
    }


    @Override
    public SysUser getUserById(Long id) {
        // TODO: 实现根据ID查询用户的逻辑
        return null;
    }

    @Override
    public List<SysUser> getAllUsers() {
        // TODO: 实现查询所有用户的逻辑
        return null;
    }



    @Override
    public void isEnable(Long id, String status) {
        // TODO: 实现用户启用禁用逻辑
        //根据id查询获取用户
        User user =userMapper.selectById(id);
        //给实体类设置状态
        user.setDataState(status);
        //传入实体类统一更新
        userMapper.updateByPrimaryKey(user);

    }

    @Override
    public List<UserVo> getUsers(UserDto userDto) {
        //TODO: 实现根据真实姓名查询用户逻辑
        //转换DTO对象为实体类对象
        User user = BeanUtil.toBean(userDto, User.class);
        List<UserVo> userVos =userMapper.selectByUser(user);
        return userVos;
    }

    @Override
    public PageResponse<UserVo> getPageUsers(Integer pageNum, Integer pageSize, UserDto userDto) {
        PageHelper.startPage(pageNum, pageSize);
        Page<User> page = userMapper.selectPageUser(userDto);
        return PageResponse.of(page, UserVo.class);
    }

    @Override
    public void resetPasswords(Long userId) {
        //使用MD5加密设置初始密码为123456
        String s = DigestUtils.md5DigestAsHex("888itcast.CN764%...".getBytes());
        User user =userMapper.selectById(userId);
        user.setPassword(s);
        userMapper.updateByPrimaryKey(user);

    }
}